/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ContentManagers.RoleManagers;

import ContentManagers.Models.Product;
import ContentManagers.Security.Validator;
import DBConnection.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Martin San Diego
 */
public class ReviewManager {
    
    public boolean addReview(String username, String product_id, String review)
    {
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();

            int i = 1;

            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO reviews (review, product_id, username, customer_id) VALUES(?,?,?,?)");

            //include parameters
            
            pstmt.setString(i++, review);
            pstmt.setString(i++, product_id);
            pstmt.setString(i++, username);
            pstmt.setInt(i++, 1);
            

            //execute query
            pstmt.executeUpdate();


            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
    public boolean checkIfReviewsExist (String product_id)
    {
        boolean check = false;
        
        try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();
          
            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM reviews WHERE product_id = ?");
            pstmt.setString(i++, product_id);
            
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {         
                check = true;
            }
            conn.close();
            
            }  catch (SQLException ex) {
                ex.printStackTrace();
            }
        
        return check;
    }
    
   public ResultSet getProductReviews(String product_id)
    {
        try {
            //get connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();

            //Construct SQL Query
            PreparedStatement pstmt;
            
            pstmt = conn.prepareStatement("SELECT * from reviews WHERE product_id = '" + Validator.inputSanitizer(product_id) + "'");
            
            //execute SQL statement
            ResultSet result = pstmt.executeQuery();

            return result;

            }
         catch (SQLException ex) {ex.printStackTrace();return null;}
    }
   
   
    
}
